package grape.composite.rest.bus.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.composite.rest.bus.form.BusListForm;
import grape.composite.service.bus.query.BusListQuery;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.composite.rest.bus.form.BusCreateForm;
import grape.composite.rest.bus.form.BusUpdateForm;
import grape.composite.rest.bus.form.BusListPageForm;
import grape.composite.rest.bus.vo.BusVo;
import grape.composite.rest.bus.mapper.BusWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.composite.service.bus.po.Bus;
import grape.composite.service.bus.api.IBusService;
import java.util.List;
/**
 * <p>
 * 公交线路 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-10
 */
@RestController
@RequestMapping("/composite/bus")
@Api(tags = "公交线路相关接口")
public class BusController extends BaseController<BusVo, Bus> {

    @Autowired
    private BusWebMapper currentWebMapper;
    @Autowired
    private IBusService currentService;



     @ApiOperation("添加公交线路")
     @PreAuthorize("hasAuthority('bus:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public BusVo create(@RequestBody @Valid BusCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }
         Bus po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询公交线路")
     @PreAuthorize("hasAuthority('bus:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public BusVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除公交线路")
     @PreAuthorize("hasAuthority('bus:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新公交线路")
     @PreAuthorize("hasAuthority('bus:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public BusVo update(@PathVariable String id,@RequestBody @Valid BusUpdateForm uf) {
         Bus po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("不分页查询公交线路")
    @PreAuthorize("hasAuthority('bus:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<BusVo> list(BusListForm listForm) {
        Bus po = currentWebMapper.formToPo(listForm);
        BusListQuery busListQuery = new BusListQuery();
        busListQuery.setPoQuery(po);
        return super.list(busListQuery);
    }

    @ApiOperation("分页查询公交线路")
    @PreAuthorize("hasAuthority('bus:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<BusVo> listPage(BusListPageForm listPageForm) {
         Bus po = currentWebMapper.formToPo(listPageForm);
        BusListQuery busListQuery = new BusListQuery();
        busListQuery.setPoQuery(po);
         return super.listPage(busListQuery,listPageForm);
     }

}
